package com.sanjishu.enterlab.electricity.sale.service;

import com.sanjishu.enterlab.electricity.sale.controller.Response;
import com.sanjishu.enterlab.electricity.sale.domain.CompanyInfo;
import com.sanjishu.enterlab.electricity.sale.domain.ContractInfo;
import com.sanjishu.enterlab.electricity.sale.domain.DetailInfo;
import com.sanjishu.enterlab.electricity.sale.mapper.CompanyInfoMapper;
import com.sanjishu.enterlab.electricity.sale.mapper.ContractInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class CompanySaleService {

    @Autowired
    private CompanyInfoService companyInfoService;
    @Autowired
    private DetailInfoService detailInfoService;
    @Autowired
    private ContractInfoService contractInfoService;
    @Autowired
    private PdfService pdfService;
    @Autowired
    private CompanyInfoMapper companyInfoMapper;
    @Autowired
    private ContractInfoMapper contractInfoMapper;

    @Transactional
    public Response generalContractAuto(String companyName, String creditCode, String operName, String address, String elecFor,
                                        String contractName, String contractPhone, String wxNum, Integer cityId, String capital,
                                        String voltageStep, String elecAccount, String transformerCapacity,
                                        String elecConsumption, String detailCategory, String bankName,
                                        String bankAccountName, String bankAccount, String contactPhone,
                                        String fax, String tradeCycle, String startAt, String endedAt,
                                        String description, Integer settingType, BigDecimal customerRate,
                                        BigDecimal bottomPrice, Integer userId, Integer companyBakId) throws Exception {
        CompanyInfo companyInfo = companyInfoService.createCompany(companyName, creditCode, operName, address, elecFor,
                contractName, contractPhone, wxNum, capital,
                null, null, null, null, null,
                cityId, userId, new Date(), companyBakId);
        DetailInfo detailInfo = detailInfoService.createDetailInfo(companyInfo.getId(), voltageStep,
                elecAccount, transformerCapacity, elecConsumption, cityId, detailCategory,
                bankName, bankAccountName, bankAccount, contactPhone, fax);

        ContractInfo contractInfo = contractInfoService.createContractInfo(companyInfo.getId(),
                null, settingType, elecConsumption, description, customerRate,
                bottomPrice, null, startAt, endedAt, tradeCycle);
        if (settingType == 3 || (settingType == 4 && bottomPrice != null && bottomPrice.compareTo(new BigDecimal(2))<=0) ) {
            companyInfo.setStatus(CompanyInfo.STATUS_VERIFIED);
            companyInfo.setUpdatedAt(new Date());
            companyInfoMapper.updateByPrimaryKey(companyInfo);
            contractInfo.setStatus(ContractInfo.STATUS_VERIFIED);
            contractInfo.setUpdateAt(new Date());
            contractInfoMapper.updateByPrimaryKey(contractInfo);
            return pdfService.generalContract(contractInfo.getId());
        }
        return Response.buildSuccess("创建成功，请等待审核");
    }

    @Transactional
    public Response updateCompanyInfo(Integer companyId, Integer contractId, Integer detailId,
                                      String companyName, String creditCode, String operName, String address, String elecFor,
                                      String contractName, String contractPhone, String wxNum, Integer cityId, String capital,
                                      String voltageStep, String elecAccount, String transformerCapacity,
                                      String elecConsumption, String detailCategory, String bankName,
                                      String bankAccountName, String bankAccount, String contactPhone,
                                      String fax, String tradeCycle, String startAt, String endedAt,
                                      String description, Integer settingType, BigDecimal customerRate,
                                      BigDecimal bottomPrice, Integer userId) throws Exception {
        CompanyInfo companyInfo = companyInfoService.updateCompany(companyId, companyName, creditCode,
                operName, address, elecFor, contractName, contractPhone, wxNum, capital,
                null, null, null, null, cityId, userId, null);
        DetailInfo detailInfo = detailInfoService.updateDetailInfo(detailId, companyId, voltageStep,
                elecAccount, transformerCapacity, elecConsumption, cityId, detailCategory,
                bankName, bankAccountName, bankAccount, contactPhone, fax);
        ContractInfo contractInfo = contractInfoService.createContractInfo(companyId, contractId, settingType,
                elecConsumption, description, customerRate,
                bottomPrice, null, startAt, endedAt, tradeCycle);
        if (settingType == 3 || (settingType == 4 && bottomPrice != null && bottomPrice.compareTo(new BigDecimal(2))<=0) ) {
            companyInfo.setStatus(CompanyInfo.STATUS_VERIFIED);
            companyInfo.setUpdatedAt(new Date());
            companyInfoMapper.updateByPrimaryKey(companyInfo);
            contractInfo.setStatus(ContractInfo.STATUS_VERIFIED);
            contractInfoMapper.updateByPrimaryKey(contractInfo);
            return pdfService.generalContract(contractInfo.getId());
        }
        return Response.buildSuccess("修改成功，请等待审核");
    }


}
